package com.zb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zb.entity.Car;
import com.zb.entity.Mr;
import com.zb.entity.User;
import com.zb.entity.Wx;
import com.zb.service.MrService;
import com.zb.serviceimpl.MrServiceImpl;
import com.zb.util.PageUtil;

/**
 * Servlet implementation class MrServlet
 */
public class MrServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String op = request.getParameter("op");
		if (op.equals("show")) {
			this.doshow(request, response);
		} else if (op.equals("toupdate")) {
			this.toupdate(request, response);
		} else if (op.equals("doupdate")) {
			this.doupdate(request, response);
		}else if(op.equals("del")) {
			this.del(request, response);
		}
	}

	protected void doshow(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		MrService mrService = new MrServiceImpl();
		int index = 1;
		String carnum = null;
		if (request.getParameter("index") != null && !"".equals(request.getParameter("index"))) {
			index = Integer.parseInt(request.getParameter("index"));
		}
		carnum = request.getParameter("carnum");
		PageUtil<Mr> page = mrService.getMrPage(index, carnum);
		request.setAttribute("mypage", page);

		request.getRequestDispatcher("search_mr.jsp").forward(request, response);

	}

	protected void toupdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		MrService mrService = new MrServiceImpl();
		int id = Integer.parseInt(request.getParameter("id"));

		Mr m = mrService.getMrById(id);
		request.setAttribute("p", m);
		request.getRequestDispatcher("Mrupdate.jsp").forward(request, response);
	}

	protected void doupdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String FeatExp = request.getParameter("FeatExp");
		String FeatName = request.getParameter("FeatName");
		String Date = request.getParameter("Date");
		String money = request.getParameter("money");
		String remake = request.getParameter("remake");
		MrService mrService = new MrServiceImpl();
		Mr m = new Mr();
		Car c = new Car();
		c.setId(id);
		m.setFeatExp(FeatExp);
		m.setFeatDate(Date);
		m.setFeatMoney(money);
		m.setRemake(remake);
		m.setCar(c);
		mrService.updateMr(m);

		response.sendRedirect("Mr.do?op=show");
	}

	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MrService mrService = new MrServiceImpl();
		int nid = Integer.parseInt(request.getParameter("id"));

		mrService.delMr(nid);
		response.sendRedirect("Mr.do?op=show");
	}
}
